package com.flydog.taskManager.common.vaildator;

import com.flydog.taskManager.dto.PhasedTaskParam;
import com.flydog.taskManager.dto.TaskBriefParam;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 简报类约束实现
 *
 * @author caoweidong
 * @version 0.0.0
 * @create 2021/7/1 15:30
 */
public class BriefValidatorClass implements ConstraintValidator<BriefValidator, TaskBriefParam> {
    @Override
    public void initialize(BriefValidator constraintAnnotation) {

    }

    @Override
    public boolean isValid(TaskBriefParam param, ConstraintValidatorContext constraintValidatorContext) {
        if (param.getPeriodicTaskId() == null && param.getPhaseTaskId() == null) {
            constraintValidatorContext
                    .buildConstraintViolationWithTemplate("phaseTaskId 和 periodicTaskId 不能同时为空")
                    .addPropertyNode("phaseTaskId")
                    .addConstraintViolation()
                    .buildConstraintViolationWithTemplate("phaseTaskId 和 periodicTaskId 不能同时为空")
                    .addPropertyNode("periodicTaskId")
                    .addConstraintViolation();
            return false;
        }
        return true;
    }
}
